/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LENOVO
 * Date: 2024-08-06
 * Time: 20:11
 */
public class MySingleLinkedList implements IOperations{
    public class Node{
        int val;
        Node next;
        public Node(int val) {
            this.val = val;
        }
    }
    public Node head;

    private void checkIndex(int index) throws IndexException{
        if(index < 0 || index > size()){
            throw new IndexException("索引异常");
        }
    }

    @Override
    public void creatLinkedList() {
        Node node1 = new Node(1);
        Node node2 = new Node(2);
        Node node3 = new Node(3);
        Node node4 = new Node(4);
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        this.head = node1;
    }

    @Override
    public void display() {
        Node cur = this.head;
        while(cur != null){
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    @Override
    public void addFirst(int val) {
        Node node = new Node(val);
        node.next = head;
        head = node;
    }

    @Override
    public void addTail(int val) {
        Node node = new Node(val);
        if(head == null){
            head = node;
            return;
        }
        Node cur = this.head;
        while(cur.next != null){
            cur = cur.next;
        }
        cur.next = node;
    }

    @Override
    public boolean contains(int val) {
        Node cur = this.head;
        while(cur != null){
            if(cur.val == val){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    @Override
    public int size() {
        Node cur = this.head;
        int size = 0;
        while(cur != null){
            cur = cur.next;
            size++;
        }
        return size;
    }

    @Override
    public void addWithIndex(int index, int val) {
        if(index == 0){
            addFirst(val);
            return;
        }
        if (index == size()){
            addTail(val);
            return;
        }
        checkIndex(index);
        int pos = 0;
        Node node = new Node(val);
        Node cur = this.head;
        while(pos != index - 1){  //计数器方法走n - 1步
            cur = cur.next;
            pos++;
        }
        //
/*        while(index - 1 != 0){  要想走n - 1步，也可以直接放到while里去--
            cur = cur.next;
            index--;
        }*/
        node.next = cur.next;
        cur.next = node;
    }

    @Override
    public void deleteEle(int ele) {
        Node cur = head;
        if(head.val == ele){
            head = head.next;
        }
        while(true){
            if(cur.next == null){
                System.out.println("没有" + ele +"值");
                break;
            }
            if(cur.next.val == ele){  //找不了第一个
                cur.next = cur.next.next;
                break;
            }
            cur = cur.next;
        }
    }

    @Override
    public void deleteAllEle(int ele) {
        Node cur = head;
        while(cur.next != null){
/*            if(head.val == ele){
                head = head.next;
            }*/
            if(cur.next.val == ele){
                Node previousNode = cur;
                cur.next = cur.next.next;
                continue;
            }
            cur = cur.next;
            if(cur == null){
                System.out.println("没有" + ele +"值");
            }
        }
        if(head.val == ele){//放到最后这个方法太巧妙了！因为我们只无法处理第一个元素，到最后直接忽略第一个元素就可以了
            head = head.next;
        }
    }
}
